package com.siwei.infoextract.wrapper.parser;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.html.HTML.Tag;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class IgnoreTags {
	public static Set<String> tags = null;

	static{
		tags = new HashSet<String>();

		try {
			InputStream ignoreTagsConfigs = IgnoreTags.class.getResourceAsStream("/Parser.config");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document document = builder.parse(ignoreTagsConfigs);
			NodeList nl = document.getElementsByTagName("Tag");

			for (int i = 0; i < nl.getLength(); i++) {
				tags.add(nl.item(i).getChildNodes().item(0).getNodeValue()); 
			}
		} catch (Exception e) { 
			// ignore the exception here, it means we don't remove any tag during normalization
		}
	}

	public static boolean contains(Tag tag) {
		return contains(tag.toString());
	}
	
	public static boolean contains(String tagName) {
		return tags.contains(tagName.toLowerCase());
	}

	public static List<Pattern> exportRegex() {		
		List<Pattern> patternList = new ArrayList<Pattern>(tags.size());

		String tagName;
		String regex;

		for (Iterator<String> iter = tags.iterator(); iter.hasNext();) {
			tagName = (String)iter.next();
			regex = "<" + tagName + ".*?>.*?</" + tagName + ">";
			
			patternList.add(Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE));
		}

		return patternList;
	}

	public static void main(String[] args) {
		
	}

	public static StringBuffer removeIgnoreTags(String htmlContent) {
		StringBuffer cleanedHtml = new StringBuffer();
		List<Pattern> patterns = exportRegex();
		for (Iterator<Pattern> iter = patterns.iterator(); iter.hasNext();) {
			cleanedHtml.delete(0, cleanedHtml.length());
			Pattern pattern = (Pattern)iter.next();
			Matcher matcher = pattern.matcher(htmlContent);
			
			while(matcher.find()) {
				matcher.appendReplacement(cleanedHtml, "");
			}
			matcher.appendTail(cleanedHtml);
			htmlContent = cleanedHtml.toString();			
		}
		return cleanedHtml;
	}
	
}